package dao.borrow;

import dao.DaoException;
import model.Borrow;
import model.BorrowDetail;

import java.sql.*;
import java.util.ArrayList;

public class BorrowDaoImpl implements BorrowDao {
    @Override
    public ArrayList<BorrowDetail> showReaderBorrowByPage(int rid, int pageNum, int pageSize) throws DaoException {

        int start = (pageNum - 1) * pageSize;

        String sql = "SELECT Rid, Bid, book.name, price, writer,classify,ReturnTime FROM `borrow`, book WHERE Rid = ? AND borrow.Bid = book.id limit ?,?";
        ArrayList<BorrowDetail> borrowDetails = new ArrayList<>();
        try (
                Connection connection = getConnection();
                PreparedStatement pstmt = connection.prepareStatement(sql);
        ) {
            pstmt.setInt(1, rid);
            pstmt.setInt(2, start);
            pstmt.setInt(3, pageSize);
            ResultSet rst = pstmt.executeQuery();
            while (rst.next()) {
                BorrowDetail borrowDetail = new BorrowDetail();

                borrowDetail.setRid(rst.getInt("Rid"));
                borrowDetail.setBid(rst.getInt("Bid"));
                borrowDetail.setName(rst.getString("name"));
                borrowDetail.setClassify(rst.getString("classify"));
                borrowDetail.setPrice(rst.getFloat("price"));
                borrowDetail.setWriter(rst.getString("writer"));
                borrowDetail.setReturnTime(rst.getTimestamp("ReturnTime"));
                borrowDetails.add(borrowDetail);
            }
            return borrowDetails;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public int findReaderBorrowNum(int rid) throws DaoException {
        String sql = "SELECT COUNT(*) as total FROM `borrow`, book WHERE Rid = ? AND borrow.Bid = book.id";

        try (Connection conn = getConnection();
             PreparedStatement preparedStatement = conn.prepareStatement(sql);

        ) {
            preparedStatement.setInt(1, rid);
            ResultSet resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
                return resultSet.getInt("total");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public void borrowBook(Borrow borrow) throws DaoException {
        String sql = "INSERT INTO borrow VALUES(?,?,?,?)";

        try (
                Connection conn = getConnection();
                PreparedStatement pstmt = conn.prepareStatement(sql);
        ) {
            pstmt.setInt(1, borrow.getRid());
            pstmt.setInt(2, borrow.getBid());
            pstmt.setTimestamp(3, new Timestamp(borrow.getBorrowTime().getTime()));
            pstmt.setTimestamp(4, new Timestamp(borrow.getReturnTime().getTime()));
            pstmt.execute();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void returnBook(int rid, int bid) throws DaoException {
        String sql = "DELETE FROM borrow WHERE Rid=? AND Bid=?";
        try (
                Connection conn = getConnection();
                PreparedStatement pstmt = conn.prepareStatement(sql);
        ) {
            pstmt.setInt(1, rid);
            pstmt.setInt(2, bid);
            pstmt.execute();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
